﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PatternWell.PAC.SL;
using PatternWell.PAC;
namespace $safeprojectname$.Controller
{
    public partial class MainController:Contract.PAC.IMainPACContract
    {
        #region Logging Helper

        /// <summary>
        /// Partial helper method that will capture an exception and make it avaliable for logging.
        /// </summary>
        /// <param name="exception">The source exception that has occured.</param>
        /// <param name="methodQualifiedName">The fully qualified name of the method where the exception was caught.</param>
        partial void LogContractError(System.Exception exception, string methodQualifiedName);

        #endregion

        #region Dispose Implementation - Implementation of the disposal helper method (Do not modify)

        /// <summary>
        /// Helper method that releases resources for this class implementation.
        /// </summary>
        /// <param name="disposed">flag that determines if the class has already been disposed.</param>
        protected override void Dispose(bool disposed)
        {
            try
            {
                ReleaseResourcesForMainController();
                base.Dispose(disposed);
            }
            catch (System.Exception disposeError)
            {
                LogContractError(disposeError, "$safeprojectname$.MainController.Dispose");
            }
        }

        #endregion

        #region Release Management - Logic used to release resources for this class (Add release logic in the supplied helper methods)

        /// <summary>
        /// Helper method that releases resource for the 'MainController' class.
        /// </summary>
        private void ReleaseResourcesForMainController()
        {
            try
            {
                //Helper method will be executed if the partial method has been implemented. Otherwise it will be removed from the compile step.
                ReleasePACResources();

                ReleasePACContract();
            }
            catch (System.Exception releaseError)
            {
                LogContractError(releaseError, "$safeprojectname$.MainController.ReleaseResourcesForMainController");
            }
        }

        /// <summary>
        /// Helper method that gets called before the PAC contract resources are released.
        /// </summary>
        partial void ReleasePACResources();

        #endregion

        #region PAC Contract Initialization

        public override void InitializePac()
        {
            try
            {
                base.InitializePac();

                //Setting the current date to the view date property in the view model.
                View.ViewModel.ViewDate = DateTime.Now.ToShortDateString();
            }
            catch (System.Exception initializeError)
            {
                LogContractError(initializeError, "$safeprojectname$.MainController.InitializePac");
            }
        }

        #endregion

        #region PAC Contract Release

        /// <summary>
        /// Helper method that releases all the resources in the pac contract.
        /// </summary>
        private void ReleasePACContract()
        {
            try
            {
                //Helper method will be executed if the partial method has been implemented. Otherwise it will be removed from the compile step.
                CleanupController();

                //TODO:MainController - ReleasePACContract
                //Release all contract properties
                //Release all contract events

            }
            catch (System.Exception releaseError)
            {
                LogContractError(releaseError, "$safeprojectname$.MainController.ReleasePACContract");
            }
        }

        /// <summary>
        /// Helper method that runs additional release logic for the contract that is not related to the PAC contract. 
        /// </summary>
        partial void CleanupController();

        #endregion

        #region PAC Contract

        //TODO:MainController - Implement PAC contract
        //Implement pac contract items here

        #endregion

        #region Child PACs

        //TODO:MainController - Implement Child PAC's here 
        //Implement logic to host child pac implementations here
        //
        #endregion

        #region Managed View

        /// <summary>
        /// View that is managed by this controller.
        /// </summary>
        protected Contract.View.IMainViewContract View
        {
            get { return _controlledView as Contract.View.IMainViewContract; }
        }

        /// <summary>
        /// Helper method that will assign a view to be managed by the controller. 
        /// </summary>
        /// <remarks>
        /// You need to make sure the view is already been created before assigning it to the controller.
        /// </remarks>
        /// <param name="view">The instance of the view cast to the supported view resource interface by this controller.</param>
        public void AttachView(Contract.View.IMainViewContract view)
        {
            try
            {
                base.AttachViewToController(view as IViewContract);
            }
            catch (System.Exception attachError)
            {
                LogContractError(attachError, "MainController.AttachView");
            }
        }

        /// <summary>
        /// Helper method that will tell the controller to drop all references to the view, and to notify the view to release viewmodel resources.
        /// </summary>
        /// <returns>Returns the reference to the view that has been detached.</returns>
        public Contract.View.IMainViewContract DetachView()
        {
            Contract.View.IMainViewContract view = null;

            try
            {
                return base.DetachControlledView() as Contract.View.IMainViewContract;
            }
            catch (System.Exception detachError)
            {
                LogContractError(detachError, "MainController.DetachView");
            }

            return view;
        }

        /// <summary>
        /// The view that is managed by the hosting PAC.
        /// </summary>
        public FrameworkElement ManagedView
        {
            get { return _controlledView as FrameworkElement; }
        }

        /// <summary>
        /// Subscribes to the events and commands that provided in the view contract.
        /// </summary>
        protected override void SubscribeToViewResources()
        {
            try
            {
                base.SubscribeToViewResources();

                //TODO:ControllerTemplate - SubscribeToViewResources
                //subscribe to the events and commands on the attached view

            }
            catch (System.Exception subcribeError)
            {
                LogContractError(subcribeError, "MainController.SubscribeToViewResources");
            }
        }

        /// <summary>
        /// Releases all subscriptions to the views commands and events and data objects provided by the view.
        /// </summary>
        protected override void ReleaseViewResources()
        {
            try
            {
                base.ReleaseViewResources();

                //TODO:ControllerTemplate - ReleaseViewResources
                // drop references to the events and commands on the attached view
            }
            catch (System.Exception releaseViewError)
            {
                LogContractError(releaseViewError, "MainController.ReleaseViewResources");
            }
        }

        #endregion

    }
}
